package com.cuit.controller;

import com.cuit.doamin.Staff;
import com.cuit.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;

    @RequestMapping("/add")
    public ModelAndView add(Staff staff) {
        ModelAndView mv = new ModelAndView();
        boolean b = staffService.add(staff);
        if (b) {
            mv.setViewName("pages/success");
        } else {
            mv.setViewName("pages/error");
        }
        return mv;
    }

    @RequestMapping("/delete")
    public ModelAndView add(Integer s_id) {
        ModelAndView mv = new ModelAndView();
        boolean b = staffService.delete(s_id);
        if (b) {
            mv.setViewName("pages/success");
        } else {
            mv.setViewName("pages/error");
        }
        return mv;
    }

    @RequestMapping("/findAll")
    public ModelAndView findAll() {
        ModelAndView mv = new ModelAndView();
        List<Staff> list = staffService.findAll();

        mv.addObject("list", list);
        mv.setViewName("admin/find_staff");
        return mv;
    }

    @RequestMapping("/update")
    public ModelAndView update(Staff staff) {
        ModelAndView mv = new ModelAndView();
        boolean b = staffService.update(staff);

        if (b) {
            mv.setViewName("pages/success");
        } else {
            mv.setViewName("pages/error");
        }
        return mv;
    }

    @RequestMapping("/findPosition")
    public ModelAndView findPosition(String position) {
        ModelAndView mv = new ModelAndView();
        List<Staff> list = staffService.findPosition("%" + position + "%");

        mv.addObject("list", list);
        mv.setViewName("admin/find_position");
        return mv;
    }
}
